package org.elepower.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.elepower.dao.PsInspectorDetailsDao;
import org.elepower.dao.SolverDao;
import org.elepower.pojo.PsInspectiontask_main;
import org.elepower.pojo.PsInspectorDetails;
import org.elepower.pojo.PsSolverDetail;
import org.elepower.pojo.PsUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/inspector")
public class InspectorController {
    @Autowired
    private PsInspectorDetailsDao psInspectorDetailsDao;
    @RequestMapping("/getNormalInspector")
    @ResponseBody
    public List<PsInspectorDetails> getNormalInspector() throws Exception{
        return psInspectorDetailsDao.getNormal();
    }
    @RequestMapping("/index")
    public String index() throws Exception{
        return "system/inspector/index";
    }
    @RequestMapping("/form")
    public String form() throws Exception{
        return "system/inspector/form";
    }
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer limit, PsInspectorDetails condition, HttpServletRequest request) throws Exception {

        Map<String, Object> map = new HashMap<>();

        Page<PsInspectorDetails> pager = PageHelper.startPage(page, limit);
        List<PsInspectorDetails> list = psInspectorDetailsDao.getlist(condition);
        request.setAttribute("list",list);
        map.put("code", 0);
        map.put("msg", "");
        map.put("data", list);
        map.put("count", pager.getTotal());
        return map;
    }

    @RequestMapping("/add")
    @ResponseBody
    public Map<String,Object> add(PsInspectorDetails psInspectorDetails, HttpSession session) throws Exception{
        Map<String,Object> map = new HashMap<>();
        PsUser user = (PsUser)session.getAttribute(Constants.LOGIN_USER);
        psInspectorDetails.setCreatedBy(user.getUsername());

        //添加巡检员信息
        psInspectorDetailsDao.add(psInspectorDetails);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Map<String,Object> update(PsInspectorDetails psInspectorDetails) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //修改巡检员信息
        psInspectorDetailsDao.update(psInspectorDetails);
        map.put("result",true);
        return map;
    }

    @RequestMapping("/get")
    @ResponseBody
    public PsInspectorDetails get(Integer id) throws Exception{
        return psInspectorDetailsDao.get(id);
    }

    @RequestMapping("/del")
    @ResponseBody
    public Map<String,Object> del(Integer id) throws Exception{
        Map<String,Object> map = new HashMap<>();
        //删除巡检员
        psInspectorDetailsDao.del(id);
        map.put("result",true);
        return map;
    }
}
